// pages/driver/leave_word/leave_word.js
Page({
  data:{
    targetID: '',
    targetType: '',
    IDD: '',
    list: [],
    inputMessage: '',
    intoView: ''
  },
  onLoad:function(options){
      console.log(options);
      // 页面初始化 options为页面跳转所带来的参数
      this.setData({
          targetID: options.targetID,
          targetType: options.targetType,
          IDD: options.IDD,
          userInfo: wx.getStorageSync('userInfo')
      });

      getServiceData(this,1);
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  sendMessage:function(){
      var that = this;
      var userInfo = wx.getStorageSync('userInfo');
      var userType = wx.getStorageSync('userType');

      wx.request({
        url: 'https://www.lanshuicar.com/Server/AllMessageInfoAndSend.ashx',
        data: {
          ID: userInfo.Id,
          type: 'set',
          idd: that.data.IDD,
          rid: that.data.targetID,
          rtype: that.data.targetType,
          retype: userType,
          content: that.data.inputMessage
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
            // success
            var code = res.data.code;
            if(code == 0){
                that.setData({
                    list: that.data.list.concat(res.data.data),
                    noMoreData: false,
                    loadError: false
                });

                setTimeout(function () {
                    that.setData({
                        intoView: res.data.data[0].rId
                    });
                },100);
            }
        },
        fail: function() {
          // fail
        },
        complete: function() {
            // complete
        }
      });

      that.setData({
          inputMessage: ''
      });

    //   this.setData({
    //       list: this.data.list.concat({
    //         "rId": rId1,
    //         "mSenderID": userInfo.Id,
    //         "mSender": userInfo.sName,
    //         "mSenderTel": userInfo.sTel,
    //         "mContent": this.data.inputMessage,
    //         "mCreateTime": new Date().format("yyyy-MM-dd hh:mm:ss"),
    //         "SenderType": userType
    //       }),
    //   });

      
  },

  inputTyping: function (e) {
      this.setData({
          inputMessage: e.detail.value
      });
  },

  clearInput:function(){
      this.setData({
          inputMessage: ''
      });
  }
})

function getServiceData(that,pageIndex){
    wx.showToast({
        title: '正在加载...',
        icon: 'loading',
        duration: 10000
    });

    if(pageIndex == 1){
        that.setData({
            list: []
        });
    }

    var userInfo = wx.getStorageSync('userInfo');
    var userType = wx.getStorageSync('userType');
    wx.request({
      url: 'https://www.lanshuicar.com/Server/AllMessageInfoAndSend.ashx',
      data: {
          ID: userInfo.Id,
          type: 'get',
          idd: that.data.IDD,
          rid: that.data.targetID,
          rtype: that.data.targetType,
          retype: userType,
          pageIndex: pageIndex,
          pageSize: 20
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
          // success
          console.log(res);
          var code = res.data.code;
          if(code == 0){
              if(res.data.data.length > 0){
                  that.setData({
                      list: that.data.list.concat(res.data.data),
                      noMoreData: false,
                  });

                  setTimeout(function () {
                      that.setData({
                          intoView: that.data.list[that.data.list.length -1].rId
                      });
                  },100);
                  
              }else{
                  that.setData({
                      noMoreData: true
                  });
              }
          }else{
              that.setData({
                  loadError: true
              });
          }
      },
      fail: function() {
          // fail
          that.setData({
              loadError: true
          });
      },
      complete: function() {
          // complete
          wx.hideToast();
          wx.stopPullDownRefresh();
      }
    })
}

Date.prototype.format = function(fmt) {
     var o = {
        "M+" : this.getMonth()+1,                 //月份 
        "d+" : this.getDate(),                    //日 
        "h+" : this.getHours(),                   //小时 
        "m+" : this.getMinutes(),                 //分 
        "s+" : this.getSeconds(),                 //秒 
        "q+" : Math.floor((this.getMonth()+3)/3), //季度 
        "S"  : this.getMilliseconds()             //毫秒 
    };
    if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
    }
     for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
             fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
         }
     }
    return fmt;
}